﻿@{
    // Cache the image for a minute
    Response.OutputCache(60);

    var photoId = UrlData[0].AsInt();
    var db = Database.Open("PhotoGallery");
    var photo = db.QuerySingle("SELECT * FROM Photos WHERE Id = @0", photoId);
    if (photo == null) {
        Response.SetStatus(HttpStatusCode.NotFound);
        return;
    }

    var size = Request["size"] ?? "";

    var width = 0;
    var height = 0;
    switch (size.ToUpperInvariant()) {
        case "":
        case "SMALL":
            width = 200;
            height = 200;
            break;
        case "MEDIUM":
            width = 400;
            height = 300;
            break;
        case "LARGE":
            width = 625;
            height = 625;
            break;
        default:
            Response.SetStatus(HttpStatusCode.BadRequest);
            return;
    }

    var image = new WebImage(photo.FileContents);
    image.Resize(width, height).Write();
}